<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <#include "/pubHeadStyleTree.html">
</head>
<body>
<title>流程定义管理</title>
<div style="position: relative;height: 100%;">
    <div class="ccpui-trees" >
        <p class="isShowTree colorSwitch">
            <span style="float: left;color: #fff">&nbsp;&nbsp;流程分类管理</span>
            <i layadmin-event="treeFlexible" class="layui-icon layui-icon-prev"></i>
        </p>
        <div style="margin-top: 6px;" >
            <ul class="ccpui-iconTree iconTrees">
                <li><a class="iconfont iconshuaxin- zTree-refresh" title="刷新"></a></li>
               <!--  <li><a class="iconfont iconwenjianjia1-copy zTree-triangle-d" title="展开"></a></li> -->
                <li><a class="iconfont iconfolder1 zTree-triangle-r" title="收起"></a></li>
                <input type="text" style="width: 50%" class="catZtreeValue">
            </ul>
            <ul id="treeSelectGlobalType" class="ztree ccpui-listZtree"></ul>
            <div id="catRightMenus" hidden>
                <ul>
                    <li id="addNode">增加分类</li>
                    <li id="editNode">修改分类</li>
                    <li id="removeNode">删除分类</li>
                </ul>
            </div>
        </div>
    </div>
    <div class="ccpui-coreContent" style="position: absolute;top:10px;right:10px;bottom: 0;left: 250px">
        <table id="actDefModels" lay-filter="actDefModels" ></table>
    </div>
</div>

<script type="text/html" id="headerToolsBar">
    <div class="ccpui-enterSel">
    <div class="layui-btn-container">
        <a class="layui-btn layui-btn-sm" lay-event="query">
            <i class="layui-icon layui-icon-search"></i>
            <span>查询</span>
        </a>
        <a class="layui-btn layui-btn-danger layui-btn-sm" lay-event="del">
            <i class="layui-icon layui-icon-delete"></i>
            <span>删除</span>
        </a>
        <a class="layui-btn layui-btn-sm" lay-event="design">
            <i class="layui-icon layui-icon-picture-fine"></i>
            <span>在线流程设计</span>
        </a>
        <a class="layui-btn layui-btn-sm" lay-event="category">
            <i class="layui-icon layui-icon-set-sm"></i>
            <span>设置分类</span>
        </a>
        <a class="layui-btn layui-btn-sm" lay-event="export">
            <i class="layui-icon layui-icon-export"></i>
            <span>导出</span>
        </a>
        <a class="layui-btn layui-btn-sm" lay-event="import">
            <i class="layui-icon layui-icon-download-circle"></i>
            <span>导入</span>
        </a>
        <a class="layui-btn layui-btn-sm" lay-event="reset">
            <i class="layui-icon layui-icon-refresh-3"></i>
            <span>重置</span>
        </a>
    </div>
    <form class="layui-form" action="" lay-filter="modelQueryForm" style="margin-top:10px">
        <div class="table-top-filter">
            <div class="table-filter-alone">
                <label class="layui-form-label">标题</label>
                <input type="text" name="Q_name_SL" id="name" class="layui-input filter-value" placeholder="标题"/>
            </div>
            <div class="table-filter-alone">
                <label class="layui-form-label">流程Key</label>
                <input type="text" name="Q_modelKey_SL" id="modelKey" class="layui-input filter-value" placeholder="流程key"/>
            </div>
            <div class="table-filter-alone">
                <label class="layui-form-label">描述</label>
                <input type="text" name="Q_description_SL" id="description" class="layui-input filter-value" placeholder="描述"/>
            </div>
            <div class="table-filter-alone">
                <label class="layui-form-label">状态</label>
                <select type="text" name="Q_status_L" id="status" class="layui-input filter-value" placeholder="状态">
                    <option value="">所有</option>
                    <option value="0">未发布</option>
                    <option value="1">已发布</option>
                    <option value="2">禁用</option>
                    <option value="3">禁用实例</option>
                    <option value="4">测试</option>
                </select>
            </div>
            <div class="table-filter-alone">
                <label class="layui-form-label">创建时间</label>
                <input type="text" name="Q_createTime_DL" id="createTime" class="layui-input filter-value" placeholder="开始时间"/>
            </div>
            <div class="table-filter-alone">
                <label class="layui-form-label">至</label>
                <input type="text" name="Q_endCreateTime_DG" id="endCreateTime" class="layui-input filter-value" placeholder="结束时间"/>
            </div>
        </div>
    </form>
    </div>
</script>
<script type="text/html" id="modelBar">
    {{#  if(d.authorizeRight && d.authorizeRight.managementDel=='Y'){ }}
    <a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="del">删除</a>
    {{#  } }}
    {{#  if(d.authorizeRight && d.authorizeRight.managementEdit=='Y'){ }}
    <a class="layui-btn layui-btn-xs" lay-event="design">设计</a>
    {{#  } }}
    {{#  if(d.status ==0){ }}
    <a class="layui-btn layui-btn-xs" lay-event="deploy">发布</a>
    {{#  } }}
    {{#  if(d.authorizeRight && d.authorizeRight.managementSet=='Y'&& d.status !=0){ }}
    <a class="layui-btn layui-btn-xs" lay-event="setting" href="">设置</a>
    {{#  } }}
    {{#  if(d.authorizeRight && d.authorizeRight.managementStart=='Y'){ }}
        {{#  if(d.status==1||d.status==4){ }}
        <a class="layui-btn layui-btn-xs" lay-event="run">启动</a>
        {{#  } }}
    {{#  } }}
    {{#  if(d.authorizeRight && d.authorizeRight.managementClean=='Y' && d.status != 0){ }}
    <a class="layui-btn layui-btn-xs" lay-event="clear">清除数据</a>
    {{#  } }}
</script>

<script>
    layui.config({
        base: serverPath.systemPath + staticPath.systemPath //静态资源所在路径
    }).extend({
        index: 'index' //主入口模块
    }).use(['index','table', 'layer', 'laydate', 'form', 'util','commons','setter','totalFilter','common','admin'], function () {
        var table = layui.table;
        var layer = layui.layer;
        var util = layui.util;
        var form = layui.form;
        var $ = layui.$;
        var setter = layui.setter;
        var laydate = layui.laydate;
        var commons = layui.commons;
        var common = layui.common;
        var totalFilter = layui.totalFilter;
        // var admin = layui.admin;

        var tableH = commons.tabHeight();

        catZtree();
        loadTable(serverPath.workflowPath + setter.workflowLinkPath + 'actDefModel/list');
        function catZtree() {
            $.ajax({
                url: serverPath.systemPath + setter.sysLinkPath + "globalType/list",
                type: "post",
                dataType: 'json',
                data: {"catKey": "FLOW_TYPE", "hasRoot": 1},
                success: function (data) {
                    var zNodes = data.data;
                    var setting = {
                        view: {
                            showLine: false,
                            nameIsHTML: true, //允许name支持html
                            selectedMulti: false
                        },
                        edit: {
                            enable: false,
                            editNameSelectAll: false
                        },
                        data: {
                            simpleData: {
                                enable: true,
                                idKey: "typeId",
                                pIdKey: "parentId"

                            },
                            key: {
                                name: "typeName"
                            }
                        },

                        callback: {
                            onClick: onClickType,
                            onRightClick: OnRightClickCat
                        }
                    };
                    $.fn.zTree.init($('#treeSelectGlobalType'), setting, zNodes);
                    component.fuzzySearch('treeSelectGlobalType', '.catZtreeValue', null, true); //初始化模糊搜索方法
                },
                error: function (XMLHttpRequest, textStatus, errorThrown) {
                    layer.alert("暂无数据，请稍后操作！" + XMLHttpRequest.status);
                }
            });
        }

        //右键菜单
        function OnRightClickCat(event, treeId, treeNode) {
            if (treeNode && !treeNode.noR) {
                ztreeNode = treeNode;
                selectCatId = treeNode.typeId;
                parentIds = treeNode.parentId;
                showRMenus(event.clientX, event.clientY);
            }
        }
        function onClickType(event, treeId, treeNode) {
            var selectTypeId = treeNode.typeId;
            var nodePath = treeNode.nodePath;
            var url = serverPath.workflowPath + setter.workflowLinkPath + 'actDefModel/list?typeId=' + selectTypeId + "&nodePath=" + nodePath;
            if (null == treeNode.parentId) {
                url = serverPath.workflowPath + setter.workflowLinkPath + 'actDefModel/list';
            }
            loadTable(url);
        }

        function showRMenus(x, y) {
            $("#catRightMenus").show();
            $("#catRightMenus ul").show();
            var h = $('#catRightMenus').height();
            x += 15;
            y += 10;
            if ($(window).height() < y + h) {
                y = y - h;
            }
            $("#catRightMenus").css({"top": y + "px", "left": x + "px", "visibility": "visible"});
            $("body").bind("mousedown", onBodyMouseDown);
        }

        function onBodyMouseDown(event) {
            if (!(event.target.id == "orgRightMenu" || $(event.target).parents("#catRightMenus").length > 0)) {
                $("#catRightMenus").css({"visibility": "hidden"});
            }
        }

        //展开
        $(".iconTrees .zTree-triangle-d").click(function () {
            $.fn.zTree.getZTreeObj("treeSelectGlobalType").expandAll(true);
        });
        //收起
        $(".iconTrees .zTree-triangle-r").click(function () {
            $.fn.zTree.getZTreeObj("treeSelectGlobalType").expandAll(false);
        });
        //刷新
        $(".iconTrees .zTree-refresh").click(function () {
            catZtree();
        });


        //增加分类节点
        $("#addNode").click(function () {
            var isRoot;
            if (ztreeNode.parentId == null) {
                isRoot = 1;//根节点
            } else {
                isRoot = 0;
            }
            layer.open({
                type: 2,
                area: ['440px', '280px'],
                title: ['新增节点'],
                content: serverPath.systemPath + setter.sysLinkPath+"globalType/view/addNode" + '?isRoot=' + isRoot + '&parentId=' + selectCatId
                ,end:function () {
                    catZtree();
                }
            });
        });
        //修改节点
        $("#editNode").click(function () {
            if (ztreeNode.parentId == null) {
                layer.alert("根节点不能修改");
            } else {
                layer.open({
                    type: 2,
                    area: ['440px', '280px'],
                    title: ['修改节点'],
                    content: serverPath.systemPath + setter.sysLinkPath + "globalType/view/addNode" + '?typeId=' + selectCatId
                    ,end:function () {
                        catZtree();
                    }
                });
            }
        });
        //删除节点
        $("#removeNode").click(function () {
            if (ztreeNode.parentId == null) {
                layer.alert("根节点不能删除");
            } else {
                layer.confirm('确定删除当前节点吗？', function (index) {
                    layer.close(index);
                    $.ajax({
                        url: serverPath.systemPath  + setter.sysLinkPath + 'globalType/del',
                        type: "post",
                        dataType:"json",
                        data: {"typeId": selectCatId},
                        success: function (result) {
                            layer.alert(result.msg);
                            catZtree();
                        }
                    });
                });
            }
        });


        //第一个实例

        function loadTable(url){
            table.render({
                elem: '#actDefModels'
                , cellMinWidth: 80
                , title: '流程定义模型'
                , height: tableH
                , url: url
                , method: 'post'
                ,autoSort: false
                ,defaultToolbar: ['filter', 'print']
                , toolbar: '#headerToolsBar'
                , page: true
                , cols: [[
                    {type: 'checkbox'}
                    , {field: 'name', title: '标题', sort: true, align: 'center'}
                    , {field: 'typeName', title: '分类', sort: true, align: 'center'}
                    , {field: 'modelKey', title: '流程Key', sort: true, align: 'center'}
                    , {field: 'version', title: '版本号', sort: true,align: 'center',width:80}
                    , {
                        field: 'status', title: '发布状态',sort: true, align: 'center', templet: function (d) {
                            var status = d.status;
                            switch (status) {
                                case 0:
                                    return "未发布";
                                case 1:
                                    return "发布";
                                case 2:
                                    return "禁用";
                                case 3:
                                    return "禁用，并禁用实例";
                                case 4:
                                    return "测试";
                            }
                        }
                    }
                    , {
                        field: 'createTime', title: '创建时间', sort: true, align: 'center', templet: function (d) {
                            return util.toDateString(d.createTime, "yyyy-MM-dd HH:mm:ss");
                        }
                    }
                    , { title: '管理', toolbar: '#modelBar', width: 150, align: 'center'}
                ]]
                ,done:function () {
                    table.resize();
                    commons.reflashDate({startName: '#createTime', endName: '#endCreateTime'});
                    common.tableSelectCon('.ccpui-enterSel .layui-form input','a[lay-event=query]','.ccpui-enterSel')
                }
            });
            totalFilter.init('actDefModels');
        }
        common.tableSort("actDefModels",{show:window.Initialization});
        table.on('toolbar(actDefModels)', function (obj) {
            var checkStatus = table.checkStatus(obj.config.id);
            var data = checkStatus.data; //获取选中行数据
            switch (obj.event) {
                case 'query':
                    common.tableConditionRender("actDefModels","modelQueryForm",{where:{}},false);
                    break;
                case 'del':
                    if (data.length === 0) {
                        layer.msg('请至少选择一行数据！', {icon: 2});
                    } else {
                        var selectedIdArray = [];
                        for (var i = 0; i < data.length; i++) {
                            selectedIdArray.push(data[i].id);
                            if(data[i].authorizeRight.managementDel=="N"){
                                layer.msg('【'+data[i].name+'】无权限删除，请重新选择！', {icon: 5});
                                return ;
                            }
                        }
                        var selectedIds = selectedIdArray.join(",");

                        layer.confirm('确定删除当前数据吗？', function () {
                            $.ajax({
                                url: serverPath.workflowPath + setter.workflowLinkPath + "actDefModel/del" ,
                                data:{modelId:selectedIds,onlyVersion:"false"},
                                type: "DELETE",
                                dataType:'json',
                                success: function (data) {
                                    if (commons._filterRecord(data)) {
                                        layer.alert(data.msg, {skin: 'btn-class'},function (index1) {
                                            layer.close(index1);
                                            //执行重载
                                            table.reload('actDefModels', {
                                                page: {curr: 1, limit: 10}
                                            });
                                            totalFilter.init('actDefModels');
                                        });
                                    } else {
                                        layer.alert(data.msg);
                                    }
                                },
                                error: function (XMLHttpRequest, textStatus, errorThrown) {
                                    layer.alert("删除失败！" + XMLHttpRequest.status);
                                }
                            })
                        })
                    }
                    break;
                case 'design':
                    var isModify = true;
                    layer.open({
                        type: 2,
                        title: "流程在线设计",
                        shade: 0.3,
                        offset: ['0px', '0px'],
                        left: 200,
                        area: ['100%', '100%'],
                        content: __ctx+'/design.html#/editor/',
                        cancel: function(index, layero){
                            isModify = false;
                        },
                        end: function () {
                            if (isModify) {
                                table.reload('actDefModels', {
                                    page: {
                                        curr: 1
                                    }
                                });
                                totalFilter.init('actDefModels');
                            }
                        }
                    });
                    break;

                case 'category':
                    if (data.length === 0) {
                        layer.msg('请先选择一行数据！');
                    } else {
                        var selectedIdArray = [];
                        for (var i = 0; i < data.length; i++) {
                            selectedIdArray.push(data[i].id);
                        }
                        var selectedIds = selectedIdArray.join(",");
                        parent.layer.open({
                            type: 2,
                            title: "分类设置",
                            shade: 0.3,
                            left: 200,
                            area: ['350px', '300px'],
                            btn:['保存','取消'],
                            content: serverPath.workflowPath + setter.workflowLinkPath + "actDefModel/view/categorySet?selectedIds="+ selectedIds,
                            yes:function (index,layero) {
                                //得到iframe页的窗口对象
                                var iframeWin = parent.window[layero.find('iframe')[0]['name']];
                                iframeWin.save(reload);
                                totalFilter.init('actDefModels');
                            }
                        });
                    }
                    break;
                case 'export':
                    if (data.length === 0) {
                        layer.msg('请至少选择一行有效数据！');
                    } else {
                        layer.confirm('确定导出吗？', {
                            btn: ['是', '否'] //按钮
                        }, function (index) {
                            layer.close(index);
                            var ids = [];
                            for (var i = 0; i < data.length; i++) {
                                ids.push(data[i].id);
                            }

                            layer.open({
                                type: 2,
                                title: "导出流程定义",
                                shade: 0.3,
                                area: ['525px', '611px'],
                                content: serverPath.workflowPath + setter.workflowLinkPath + "actDefModel/view/export?ids="+ids.join(','),
                                end:function(){
                                    totalFilter.init('actDefModels');
                                }
                            });
                        });
                    }
                    break;
                case 'import':
                    var isModify = true;
                    layer.open({
                        type: 2,
                        title: "导入流程定义",
                        shade: 0.3,
                        left: 200,
                        area: ['400px', '350px'],
                        content: serverPath.workflowPath + setter.workflowLinkPath + "actDefModel/view/import",
                        cancel: function(index, layero){
                            isModify = false;
                        },
                        end:function(){
                            if (isModify) {
                                table.reload('actDefModels');
                                totalFilter.init('actDefModels');
                            }
                        }
                    });
                    break;
                case 'reset':
                    common.tableConditionRender("actDefModels","modelQueryForm", {where:{}},true);
                    break;
            }
        });
        function reload(){
            table.reload('actDefModels', {
                page: {
                    curr: 1
                }
            });
        }
        //监听行工具事件
        table.on('tool(actDefModels)', function (obj) {
            var data = obj.data;
            var modelId = data.id;
            var procDefId = data.procDefId;
            $('.layui-table-tips-main a[lay-event=setting]').attr("href",serverPath.workflowPath + setter.workflowLinkPath+"actDefModel/view/commonSetting?modelId=" + data.id);
            switch (obj.event) {
                case 'del':
                    layer.confirm("确认要删除该模型吗？", {title: '删除确认'}, function (index) {
                        $.ajax({
                            url: serverPath.workflowPath + setter.workflowLinkPath + "actDefModel/del",
                            data:{modelId:modelId,onlyVersion:"false"},
                            type: 'DELETE',
                            dataType:'json',
                            success: function (data) {
                                if (commons._filterRecord(data)) {
                                    layer.alert(data.msg, {skin: 'btn-class'},function (index1) {
                                        layer.close(index1);
                                        //执行重载
                                        table.reload('actDefModels', {
                                            page: {curr: 1, limit: 10}
                                        });
                                        totalFilter.init('actDefModels');
                                    });
                                } else {
                                    layer.alert(data.msg);
                                }
                            },
                            error: function (XMLHttpRequest, textStatus, errorThrown) {
                                layer.alert("删除失败！" + XMLHttpRequest.status);
                            }
                        });
                    });
                    break;
                case 'design':
                    var isModify = true;
                    layer.open({
                        type: 2,
                        title: "流程在线设计",
                        shade: 0.3,
                        offset: ['0px', '0px'],
                        left: 200,
                        area: ['100%', '100%'],
                        content: __ctx+'/design.html#/editor/' + data.id,
                        cancel: function(index, layero){
                            isModify = false;
                        },
                        end: function () {
                            if (isModify) {
                                table.reload('actDefModels', {
                                    page: {
                                        curr: 1
                                    }
                                });
                                totalFilter.init('actDefModels');
                            }

                        }
                    });
                    break;
                case 'deploy':
                    $.ajax({
                        url: serverPath.workflowPath + setter.workflowLinkPath + 'actDefModel/publishModel',
                        type: 'POST',
                        data: {modelId: modelId},
                        dataType: 'json',
                        success: function (result) {
                            layer.alert(result.msg);
                            table.reload('actDefModels', {
                                page: {
                                    curr: 1
                                }
                            });
                            totalFilter.init('actDefModels');
                        },
                        error: function (result) {
                            layer.alert(result.msg);
                        }
                    });
                    break;
                case 'setting':
                    var url = "actDefModel/view/commonSetting?modelId=" + data.id;
                    $(this).attr("href",serverPath.workflowPath + setter.workflowLinkPath+url)
                    // layer.open({
                    //     type: 2,
                    //     title: "流程在线设计",
                    //     shade: 0.3,
                    //     offset: ['0px', '0px'],
                    //     left: 200,
                    //     area: ['100%', '100%'],
                    //     content:  serverPath.workflowPath + setter.workflowLinkPath+url,
                    //     cancel: function(index, layero){
                    //         isModify = false;
                    //     },
                    //     end: function () {
                    //         if (isModify) {
                    //             table.reload('actDefModels', {
                    //                 page: {
                    //                     curr: 1
                    //                 }
                    //             });
                    //             totalFilter.init('actDefModels');
                    //         }
                    //
                    //     }
                    // });

                    // window.location.href = url;
                    break;
                case 'run':
                    $.ajax({
                        url: serverPath.workflowPath + setter.workflowLinkPath + "actDefModel/getCanDirectStart?modelId="+data.id,
                        type: 'get',
                        dataType:'json',
                        success: function (result){
                            var data = result.data;
                            if(data){
                                var flowUrl= serverPath.basePath + setter.workflowLinkPath +"task/startFlow";
                                $.post(flowUrl,{procDefId:procDefId},function(result){
                                    if (result.code === 0) {
                                        layer.alert(result.msg,function(index){
                                            layer.close(index);
                                        });
                                    } else {
                                        layer.alert(result.msg);
                                    }
                                },'json');
                            }else{
                                var url= serverPath.workflowPath + setter.workflowLinkPath +'task/view/startFlowForm?modelId='+modelId;
                                parent.layer.open({
                                    type: 2,
                                    title: "流程启动",
                                    area: ['100%', '100%'],
                                    content: url
                                });
                            }
                        },
                        error: function(XMLHttpRequest, textStatus, errorThrown) {
                            layer.alert("操作失败！" + XMLHttpRequest.status);
                        }
                    });
                    break;
                case 'clear':
                    layer.confirm("确认要对该模型清除数据吗？", {title: '清除确认'}, function (index) {
                        $.ajax({
                            url: serverPath.workflowPath + setter.workflowLinkPath + "actDefModel/cleanData",
                            type: 'DELETE',
                            data:{modelId:modelId},
                            dataType: 'json',
                            success: function (res) {
                                if(res.code==0){
                                    layer.msg(res.msg,{icon:6});
                                }else{
                                    layer.msg(res.msg,{icon:5});
                                }
                            },
                            error:function () {
                                layer.msg("清除失败",{icon:5});
                            }
                        });
                    });
                    break;
            }
        });


        commons.reflashDate({startName: '#createTime', endName: '#endCreateTime'});
        window.Initialization = function () {
            commons.reflashDate({startName: '#createTime', endName: '#endCreateTime'});
        };

    });
</script>



<style>
    div#catRightMenus {
        position: fixed;
        border: 1px solid #ccc;
        z-index: 900;
        visibility: hidden;
        top: 0;
        background-color: #f5f5f5;
        text-align: left;
        padding: 2px;
    }

    div#catRightMenus ul li {
        margin: 5px 0;
        padding: 0 8px;
        cursor: pointer;
        list-style: none outside none;

    }
    .table-top-filter .layui-icon-up{
    	display:none !important
    }
</style>
</body>
</html>
<!--<link rel="stylesheet" href="${ctx}/layui/css/layui.css" media="all"/>-->
<link rel="stylesheet" href="${ctx}/systemStatic/style/admin.css">
<link rel="stylesheet" href="${ctx}/iconfont/iconfont.css"/>